import 'package:flutter/material.dart';
import '../input/InputCustom.dart';

/*
 * @class AppBarSearch
 * @author: ZhongWb
 * @date: 2019/12/179:24
 * @description AppBar重写官方  搜索框导航
 **/
class AppBarSearch extends StatefulWidget implements PreferredSizeWidget {
  final double contentHeight; //从外部指定高度
  final Color statusBarColor; //设置statusbar的颜色
  ValueChanged onChangde; //输入改变时
  ValueChanged onSubmit; //提交时
  bool isShowClear; //是否显示 清空输入框按钮
  bool isLeftIcon; //是否显示 搜索按钮
  bool isShowQuitButton; //是否显示取消按钮
  GestureTapCallback onQuitTop; //取消按钮点击
  double vertical; //控制居中
  double inputHeight; //输入框高度
  double leftPadding; //左边距
  Color fontColor; //输入框字体大小
  double fontSize; //输入框文本大小
  double rightPadding; //右边距
  TextEditingController controller; //控制器
  FocusNode focusNode; //焦点
  IconData rightIcon; //右侧图标
  Color rightIconColor; //右侧图标颜色
  double rightIconSize; //右侧图标大小
  String leftIcon; //左侧图标
  Color leftIconColor; //左侧图标颜色
  double leftIconSize; //左侧图标大小
  Color hinColor; //提示颜色
  ValueChanged onRight; //右侧图标点击事件
  String hinText;//
  Color cursorColor;//光标颜色
  double cursorWidth;//光标大小
  GestureTapCallback onTap; //点击事件
  AppBarSearch(
      {this.onTap,
        this.cursorColor,
        this.cursorWidth:1.5,
      this.isLeftIcon: true,
      this.rightIcon,
      this.hinText: "提示文本",
      this.onRight,
      this.vertical: 9.2,
      this.hinColor: Colors.grey,
      this.leftIconSize: 25,
      this.leftIconColor: Colors.grey,
      this.leftIcon,
      this.rightIconSize: 18,
      this.rightIconColor,
      this.fontColor: Colors.black45,
      this.fontSize: 14,
      this.focusNode,
      this.controller,
      this.leftPadding: 12,
      this.onQuitTop,
      this.rightPadding: 12,
      this.inputHeight: 29,
      this.isShowQuitButton: false,
      this.contentHeight: 44,
      this.statusBarColor,
      this.isShowClear: true,
      this.onSubmit,
      this.onChangde});

  @override
  Size get preferredSize => new Size.fromHeight(contentHeight);

  @override
  _AppBarSearchState createState() => _AppBarSearchState();
}

class _AppBarSearchState extends State<AppBarSearch> {
  TextEditingController _connection = new TextEditingController();
  FocusNode _focusNode = new FocusNode();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    if (widget.controller != null) {
      setState(() {
        _connection = widget.controller;
      });
    }
    if (widget.focusNode != null) {
      setState(() {
        _focusNode = widget.focusNode;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.statusBarColor ?? Color(0xff3D7ADD),
      child: SafeArea(
        top: true,
        child: Container(
          height: widget.contentHeight,
          alignment: Alignment.center,
          padding: EdgeInsets.only(left: 12, right: 12),
          child: Row(
            children: <Widget>[
              InputCustom(
                cursorColor: widget.cursorColor ,
                cursorWidth:widget.cursorWidth,
                widght: MediaQuery.of(context).size.width-(12+12+40),
                onTap: widget.onTap,
                hinText: widget.hinText,
                leftIcon: widget.leftIcon,
                leftIconColor: widget.leftIconColor,
                leftIconSize: widget.leftIconSize,
                rightIcon: widget.rightIcon,
                rightIconColor: widget.rightIconColor ??  Color(0xffDBDBDB),
                rightIconSize: widget.rightIconSize,
                fontColor: widget.fontColor,
                fontSzie: widget.fontSize,
                controller: widget.controller,
                hinColor: widget.hinColor,
                height: widget.inputHeight,
                isShowClear: widget.isShowClear,
                isLeftIcon: widget.isLeftIcon,
                vertical: widget.vertical,
                onChanged: (v) {
                  setState(() {});
                  widget.onChangde(v);
                },
                onSubmit: (v) => widget.onSubmit(v),
                onRight: (v) => widget.onRight(v),
              ),
              widget.isShowQuitButton
                  ? InkWell(
                      onTap: widget.onQuitTop,
                      child: Container(
                        width: 40,
                        alignment: Alignment.centerRight,
                        child: Text(
                          "取消",
                          style: TextStyle(fontSize: 17, color: Colors.white),
                        ),
                      ),
                    )
                  : Container(),
            ],
          ),
        ),
      ),
    );
  }
}
